package com.taojiazheng.controller.favorite;

import com.taojiazheng.common.ResultResponse;
import com.taojiazheng.dao.user.UserDao;
import com.taojiazheng.entity.Favoritejob;
import com.taojiazheng.entity.Favoriteuser;
import com.taojiazheng.entity.User;
import com.taojiazheng.service.favorite.FavoriteService;
import com.taojiazheng.so.favorite.FavoriteSo;
import com.taojiazheng.vo.favorite.FavoriteJobVo;
import com.taojiazheng.vo.favorite.FavoriteUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 赵成林
 * @date 2017/12/23 22:23
 * 用户收藏岗位/人
 * 收藏的岗位/人列表
 */
@Controller
public class FavoriteController {
    @Autowired
    private FavoriteService favoriteService;

    @Autowired
    private UserDao userDao;

    /**
     * 收藏岗位 
     * 不可重复收藏
     */
    @ResponseBody
    @RequestMapping(value = "create/favorite/job",method = RequestMethod.POST)
    public ResultResponse createFavroteJob(@RequestBody FavoriteSo so, HttpServletRequest request){
        String token = request.getHeader("token");
        User user = userDao.getUserByToken(token);
        Favoritejob favoritejob = new Favoritejob();
        favoritejob.setJobId(so.getJobId());
        favoritejob.setUserId(user.getId());
        favoriteService.createFavoriteJob(favoritejob);
        return new ResultResponse();
    }
    /**
     * 收藏人
     * 不可重复收藏
     */
    @ResponseBody
    @RequestMapping(value = "create/favorite/user",method = RequestMethod.POST)
    public ResultResponse createFavoriteUser(@RequestBody FavoriteSo so,HttpServletRequest request){
        String token = request.getHeader("token");
        User user = userDao.getUserByToken(token);
        Favoriteuser favoriteuser = new Favoriteuser();
        favoriteuser.setFavoriteUserId(so.getFavoriteUserId());
        favoriteuser.setUserId(user.getId());
        favoriteService.createFavoriteUser(favoriteuser);
        return new ResultResponse();
    }

    /**
     * 获取收藏的岗位的列表
     */
    @ResponseBody
    @RequestMapping(value = "favorite/job/get",method = RequestMethod.POST)
    public ResultResponse getFavoriteJob(HttpServletRequest request){
        List<FavoriteJobVo> favoriteJobVos = favoriteService.getJobList(request.getHeader("token"));
        return new ResultResponse(favoriteJobVos);
    }

    /**
     * 获取收藏的人的列表
     */
    @ResponseBody
    @RequestMapping(value = "favorite/user/get",method = RequestMethod.POST)
    public ResultResponse getFavoriteUser(HttpServletRequest request){
        List<FavoriteUserVo> favoriteUserVos = favoriteService.getUserList(request.getHeader("token"));
        return new ResultResponse(favoriteUserVos);
    }
    /**
     * 删除收藏的工作岗位
     */
    @ResponseBody
    @RequestMapping(value="favorite/job/delete",method = RequestMethod.POST)
    public ResultResponse deleteFavoriteJob(HttpServletRequest request,@RequestBody FavoriteSo favoriteSo){
    	favoriteService.deleteFavoriteJob(favoriteSo.getId());
    	return new ResultResponse<>(); 
    }
    /**
     * 删除收藏的服务提供者
     * @param favoriteSo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "favorite/user/delete",method = RequestMethod.POST)
    public ResultResponse deleteFavoriteUser(@RequestBody FavoriteSo favoriteSo){
    	favoriteService.deleteFavoriteUser(favoriteSo.getId());
    	return new ResultResponse<>();
    } 
}
